package io.output;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author mubei
 * @date 2021/12/3
 * @description  文件拷贝
 */
public class FIleCopy {

    // 需要复制一张照片
    @Test()
    public void test1() {
        String sourcePath = "D:\\date\\distable\\practice\\code\\basic\\src\\main\\java\\io/编译原理.jpg";
        String targetPath = "D:\\date\\distable\\practice\\code\\basic\\src\\main\\java\\io/copy.jpg";


        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        byte[] bys = new byte[512];
        int readData = 0;
        try {
            inputStream = new FileInputStream(sourcePath);
            outputStream = new FileOutputStream(targetPath);
            while((readData = inputStream.read(bys)) != -1){
                outputStream.write(bys,0,readData);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Test()
    public void test2() {
        String sourcePath = "D:\\date\\distable\\practice\\code\\basic\\src\\main\\java\\io/编译原理.jpg";
        String targetPath = "D:\\date\\distable\\practice\\code\\basic\\src\\main\\java\\io/copy.jpg";

        File file = new File(sourcePath);
        if(file.exists()) {
            file.delete();
        }
        file = new File(targetPath);
        if(file.exists()) {
            file.delete();
        }
    }
}
